﻿package
{
	import flash.display.Sprite;
	import flash.events.DatagramSocketDataEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.net.DatagramSocket;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.utils.ByteArray;
	import flash.net.InterfaceAddress; 
	import flash.net.NetworkInfo; 
	import flash.net.NetworkInterface; 
	import flash.utils.Timer;
	import flash.events.*;

	public class DatagramSocketExample extends EventDispatcher
	{
		private var datagramSocket:DatagramSocket = new DatagramSocket();
		public var respuesta:String = "";

		public function getbroadcastip():String
		{
			var netInterfaces:Vector.<NetworkInterface >  = NetworkInfo.networkInfo.findInterfaces();
			if (netInterfaces && netInterfaces.length > 0)
			{
				var i:NetworkInterface = netInterfaces[0];
				if (i.active)
				{
					var addresses:Vector.<InterfaceAddress > = i.addresses;
					var j:InterfaceAddress = addresses[0];
					var ipfinal:String = j.address;
					var iparray:Array = ipfinal.split(".", 3);
					ipfinal = iparray[0]+"."+iparray[1]+"."+iparray[2]+"."+"255";
					trace(ipfinal);
					return ipfinal;
				}
			}
			return "";
		}
		
		public function DatagramSocketExample()
		{
			
		}
		
		public function ask(){
			bind();
			send();
			//respuesta="192.168.0.20";
			//dispatchEvent(new Event("ipRecibida"));
		}

		private function bind():void
		{
			if( datagramSocket.bound ) 
			{
				datagramSocket.close();
				datagramSocket = new DatagramSocket();
			}
			//ver dir ip
			var netInterfaces:Vector.<NetworkInterface >  = NetworkInfo.networkInfo.findInterfaces();
			if (netInterfaces && netInterfaces.length > 0)
			{
				var i:NetworkInterface = netInterfaces[0];
				if (i.active)
				{
					var addresses:Vector.<InterfaceAddress > = i.addresses;
					var j:InterfaceAddress = addresses[0];
					var ipfinal:String = j.address;
					
				}
			}
			//termina ver ip
			datagramSocket.bind( 8686 , ipfinal );
			datagramSocket.addEventListener( DatagramSocketDataEvent.DATA, dataReceived );
			datagramSocket.receive();
			trace( "Bound to: " + datagramSocket.localAddress + ":" + datagramSocket.localPort );
		}

		private function dataReceived( event:DatagramSocketDataEvent )
		{
			respuesta = event.data.readUTFBytes( event.data.bytesAvailable );
            trace("Received from " + event.srcAddress + ":" + event.srcPort + "> " + respuesta);
            dispatchEvent(new Event("ipRecibida"));
        }
        public function getHostIp():String{
			return respuesta;
		}
		
        private function send():void
        {
            //Create a message in a ByteArray
            var data:ByteArray = new ByteArray();
            data.writeUTFBytes( "hola susana te estamos pingueando" );
            
            //Send a datagram to the target
            try
            {	
				var ip:String=getbroadcastip();//villereada para simular broadcast
				datagramSocket.send( data, 0, 0, "192.168.0.20", 9898); 
/*            	for(var i:int=1;i<=254;i++){
					var iparray:Array = ip.split(".", 3);
					ip = iparray[0]+"."+iparray[1]+"."+iparray[2]+"."+i;
					datagramSocket.send( data, 0, 0, ip, 9898); 
            		trace( "Sent message to "+ip+" Broadcast" + ":" + 9898 );
					}
*/				
            }
            catch ( error:Error )
            {
				trace( error.message );
            }
        }
    }
}